package sm;

import static org.junit.Assert.*;
import java.util.Hashtable;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestServiceManager {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testGetInstance() {
		ServiceManager.getInstance();
		assertEquals(3,ServiceRegistry.serviceIds.size());
		assertTrue(ServiceRegistry.serviceIds.containsKey("2"));
		assertTrue(ServiceRegistry.serviceIds.containsKey("3"));
		assertTrue(ServiceRegistry.serviceIds.containsKey("1000"));
		assertEquals(2,((Integer)ServiceRegistry.serviceIds.get("2")).intValue());
		assertEquals(3,((Integer)ServiceRegistry.serviceIds.get("3")).intValue());
		//long request20 = 31;
		//Long request20Long = new Long(request20);
		//System.out.println("long to string " + request20Long.toString());
	}
	@Test
	public void testSensingServiceConcurrent() {

		long request21 = 21;
		long request31 = 31;
		long request32 = 32;
		long samplingFreq = 1000;
		long samplingFreq2 = 2000;
		long duration = 3000;
		long sleep = 1;
		long duration2 = 5000;
		long tx = 20000;
		byte target = 000000000010;
		byte requester = 00144;
		
		System.out.println(" starting sensing service concurrent ");
		System.out.println("# of threads at start " + Thread.activeCount());
		ServiceManager serviceManager2 = ServiceManager.getInstance();		
		serviceManager2.serviceRequest(request21, "2", samplingFreq, duration, tx, "6", target, requester);
		System.out.println("# of threads after request21 " + Thread.activeCount());
		assertEquals(3,ServiceRegistry.serviceIds.size());
		assertTrue(serviceManager2.checkAvailability("2"));
		serviceManager2.serviceRequest(request31, "3", samplingFreq2, duration2, tx, "6", target, requester);
		System.out.println("# of threads after request31 " + Thread.activeCount());
		System.out.println(serviceManager2.getEventObject().getSampleFreq());
		System.out.println("# of threads after 1st short sleep " + Thread.activeCount());
		System.out.println(serviceManager2.getData(request21, "2", requester).toString());
		System.out.println(serviceManager2.getData(request31, "3", requester).toString());
		System.out.println(serviceManager2.getAllData("3", requester).toString());
		System.out.println(serviceManager2.getEventObject().getSampleFreq());
		try {
			Thread.sleep(duration);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("# of threads after duration sleep2 " + Thread.activeCount());
		System.out.println(serviceManager2.getData(request21, "2", requester).toString());
		System.out.println(serviceManager2.getData(request31, "3", requester).toString());
		System.out.println(serviceManager2.getAllData("3", requester).toString());
		System.out.println(serviceManager2.getAllData("2", requester).toString());
		serviceManager2.serviceRequest(request32, "3", samplingFreq, duration, tx, "6", target, requester);
		System.out.println("# of threads after request32 " + Thread.activeCount());
		System.out.println(serviceManager2.getAllData("3", requester).toString());		
		System.out.println(serviceManager2.getAllData("3", requester).toString());
		try {
		Thread.sleep(duration);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		System.out.println("# of threads after duration sleep3 " + Thread.activeCount());
		System.out.println(serviceManager2.getAllData("3", requester).toString());
		
	}
	@Test
	public void testServiceRequestLongStringLongLongLongStringByteByte() {
		
		long request22 = 22;
		long request33 = 33;
		long request32 = 32;
		long request34 = 34;
		long samplingFreq3 = 1000;
		long duration3 = 2000;
		long samplingFreq = 1000;
		long duration = 3000;
		long tx = 20000;
		byte target = 000000000010;
		byte requester = 00144;
		long sleep = 1000;
		
		System.out.println(" starting service requestlong.... ");
		ServiceManager serviceManager = ServiceManager.getInstance();
		serviceManager.serviceRequest(request22, "2", samplingFreq, duration, tx, "6", target, requester);
		assertEquals(3,ServiceRegistry.serviceIds.size());
		assertTrue(serviceManager.checkAvailability("2"));
		long check = 800;
		assertEquals(check, serviceManager.getEventObject().getSampleFreq());
		assertTrue(serviceManager.getEventObject().isValidFreq(samplingFreq));
		assertEquals(2, serviceManager.getEventObject().getEventId());
		Hashtable resultHash = serviceManager.getData(request22, "2", requester);
		System.out.println(resultHash.toString());
		serviceManager.serviceRequest(request33, "3", samplingFreq, duration, tx, "6", target, requester);
		System.out.println(serviceManager.getData(request33, "3", requester).toString());
		System.out.println(" data for request32 of previous method");
		System.out.println(serviceManager.getData(request32, "3", requester).toString());
		long check2 = 1000;
		assertEquals(check2, serviceManager.getEventObject().getSampleFreq());
		serviceManager.serviceRequest(request34, "3", samplingFreq3, duration3, tx, "6", target, requester);
		System.out.println(serviceManager.getData(request34, "3", requester).toString());		
		LightService light1 = LightService.getInstance();
		System.out.println(light1.getAllData().toString());
		try {
			Thread.sleep(sleep);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		//Long requestIdKey34 = new Long(request34);
		assertTrue(light1.getAllData().containsKey(request34));
		assertFalse(light1.isValidRequest(request34, samplingFreq3, duration));
		long request4 = 0004;
		long samplingFreq4 = 55;
		assertTrue(light1.isValidRequest(request4, samplingFreq3, duration));
		assertFalse(light1.isValidRequest(request4, samplingFreq4, duration3));
		try {
			Thread.sleep(duration);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		System.out.println("# of threads after duration sleep3 " + Thread.activeCount());
		System.out.println(serviceManager.getAllData("3", requester).toString());
		System.out.println(serviceManager.getAllData("2", requester).toString());
	}

	@Test
	public void testServiceRequestLongStringByteByte() {
		
		long request23 = 23;
		long request35 = 35;
		byte target = 000000000010;
		byte requester = 00144;
		
		System.out.println(" SERVICE REQUEST SENCILLO");
		System.out.println(ServiceManager.getInstance().serviceRequest(request23, "2", target, requester) + " one");
		System.out.println(ServiceManager.getInstance().serviceRequest(request35, "3", target, requester)+ " two");
	}

	@Test
	public void testCheckAvailability() {
		
		long request24 = 24;
		long request36 = 36;
		long samplingFreq = 1000;
		long duration = 3000;
		long tx = 20000;
		byte target = 000000000010;
		byte requester = 00144;
		long sleep = 5;
		
		ServiceManager serviceManager = ServiceManager.getInstance();
		System.out.println("check availability");
		assertTrue(TempService.getInstance().isValidRequest(request24, samplingFreq, duration));
		serviceManager.serviceRequest(request24, "2", samplingFreq, duration, tx, "6", target, requester);
		System.out.println(serviceManager.getData(request24, "2", requester).toString());
		try {
			Thread.sleep(sleep);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertFalse(TempService.getInstance().isValidRequest(request24, samplingFreq, duration));
		assertEquals(3,ServiceRegistry.serviceIds.size());
		assertTrue(serviceManager.checkAvailability("2"));
		serviceManager.serviceRequest(request36, "3", samplingFreq, duration, tx, "6", target, requester);
		System.out.println(serviceManager.getData(request24, "2",requester).toString());
		System.out.println(LightService.getInstance().getValue(request36));
		System.out.println(TempService.getInstance().getAllData().toString());
		System.out.println(LightService.getInstance().getAllData().toString());
		System.out.println("last");
		serviceManager.serviceRequest(request36, "3", samplingFreq, duration, tx, "6", target, requester);
		System.out.println(serviceManager.getAllData("3", requester));
		
	}	
	
	@Test
	public void testSameServiceConcurrent() {
		
		long request25 = 25;
		long request26 = 26;
		long samplingFreq = 1000;
		long samplingFreq2 = 2000;
		long duration = 1500;
		long duration2 = 4020;
		long tx = 20000;
		byte target = 000000000010;
		byte requester = 00144;
		long sleep = 2500;
		long sleep2 = 5;
		
		ServiceManager serviceManager = ServiceManager.getInstance();
		serviceManager.serviceRequest(request25, "2", samplingFreq, duration, tx, "6", target, requester);
		serviceManager.serviceRequest(request26, "2", samplingFreq2, duration2, tx, "6", target, requester);
		try {
			Thread.sleep(sleep2);
		} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		System.out.println(serviceManager.getData(request25, "2",requester).toString());
		System.out.println(serviceManager.getData(request26, "2",requester).toString());
		System.out.println("# of threads before sleep1  " + Thread.activeCount());
		try {
			Thread.sleep(duration2);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("back from sleep 1 ");
		System.out.println(serviceManager.getAllData("2", requester));
		System.out.println(serviceManager.getData(request25, "2",requester).toString());
		System.out.println(serviceManager.getData(request26, "2",requester).toString());
		try {
			Thread.sleep(sleep);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(" back from sleep 2 ");
		System.out.println("# of threads after sleep2  " + Thread.activeCount());
		
	}

	@Test
	public void testSameServiceConcurrentDirectly() {
		long request27 = 27;
		long request28 = 28;
		long request29 = 29;
		long request201 = 201;
		long samplingFreq = 1000;
		long samplingFreq2 = 2000;
		long duration = 1501;
		long duration2 = 4020;
		long tx = 20000;
		byte target = 000000000010;
		byte requester = 00144;
		long sleep = 6000;
		long sleep2 = 5;
		
		TempService tempService = TempService.getInstance();
		LightService lightService = LightService.getInstance();
		ServiceManager serviceManager = ServiceManager.getInstance();
		serviceManager.serviceRequest(request27, "2", samplingFreq, duration, tx, "6", target, requester);
		lightService.processRequestThread(request27, samplingFreq, duration);
		tempService.processRequestThread(request28, samplingFreq2, duration2);	
		serviceManager.serviceRequest(request28, "3", samplingFreq2, duration2, tx, "6", target, requester);
		tempService.processRequestThread(request29, samplingFreq, duration);
		tempService.processRequestThread(request201, samplingFreq, duration);
		try {
			Thread.sleep(sleep);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(tempService.getAllData());
		System.out.println(serviceManager.getAllData("3", requester));
		System.out.println(serviceManager.getData(request27, "2", requester));
		System.out.println(lightService.getValue(request27));
		System.out.println(tempService.getValue(request28));
		System.out.println(lightService.getValue(request28));
		System.out.println(tempService.getValue(request29));
		System.out.println(tempService.getValue(request201));
		
		

	
	}

}
